/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.buzzwords.behaviors;

import com.buzzwords.model.Buzzword;
import java.util.List;

/**
 *
 * @author Rico Hightower
 */
public class BuzzwordIDBehavior extends DatabaseBehavior
{
    public BuzzwordIDBehavior()
    {
        
    }

    @Override
    public boolean create(Object obj)
    {
        boolean buzzwordCreated = false;

        if (obj instanceof Buzzword)
        {
            //persist the value
            this.persist(obj);
        }

        return buzzwordCreated;
    }

    @Override
    public List<Object> read(Object obj)
    {
        List<Object> results = null;

        if (obj instanceof Buzzword)
        {
            //create an instance of the entity manager
            createEM();

            //search for buzzword by all possible fields
            results = em.createNamedQuery(Buzzword.class.getAnnotations()[0].toString()).getResultList();
        }

        return results;
    }

    @Override
    public boolean update(Object obj)
    {
        boolean buzzwordUpdate = false;

        //if the entity already exists, then update it, else create a new
        //entity for this buzzword
        if (obj instanceof Buzzword)
        {
            if (read(obj) == null || read(obj).isEmpty())
            {
                create(obj);
                buzzwordUpdate = true;
            }
            else
            {
                createEM();
                em.merge(obj);
                buzzwordUpdate = true;
            }
        }

        return buzzwordUpdate;
    }

    @Override
    public boolean delete(Object obj)
    {
        boolean buzzwordDeleted = false;

        if (obj instanceof Buzzword)
        {
            this.remove(obj);
            buzzwordDeleted = true;
        }

        return buzzwordDeleted;
    }
}
